using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace C4F.DevKit.P2PPictureShare.PictureShareWpf
{
    // Handles operations for the configuration window.
    public partial class ConfigurationWindow : System.Windows.Window
    {
        public ConfigurationWindow()
        {
            // Init generated code.
            InitializeComponent();

            // Fill in text boxes with settings information.
            this.textBoxCacheFolder.Text = SettingsWrapper.CacheFolder;
            this.textBoxShareFolder.Text = SettingsWrapper.ShareFolder;
            this.textBoxPictureSize.Text = SettingsWrapper.MaxPictureSize.ToString();
            this.textBoxP2PAddress.Text = SettingsWrapper.P2PAddress;
            this.textBoxP2PPassword.Text = SettingsWrapper.P2PPassword;
        }

        // Called when user clicks the buttonSelectCache Button (see XAML)
        void OnSelectCacheFolder(object sender, RoutedEventArgs e)
        {
            const string action = "Select the folder to store incoming pictures";

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = action;

            // Present folder browser window to user and wait for result.
            DialogResult result = fbd.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                // Update text box.
                this.textBoxCacheFolder.Text = fbd.SelectedPath;
            }
        }

        // Called when user clicks the buttonSelectShare Button (see XAML)
        void OnSelectShareFolder(object sender, RoutedEventArgs e)
        {
            const string action = "Select the folder containing pictures to share";

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = action;

            // Present folder browser window to user and wait for result.
            DialogResult result = fbd.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                // Update text box.
                this.textBoxShareFolder.Text = fbd.SelectedPath;
            }
        }

        // Called when user clicks the buttonSaveSettings Button (see XAML)
        void OnSaveSettings(object sender, RoutedEventArgs e)
        {
            // Note: we only save if the new values have changed.

            if (this.textBoxCacheFolder.Text != SettingsWrapper.CacheFolder)
            { SettingsWrapper.CacheFolder = this.textBoxCacheFolder.Text; }

            if (this.textBoxShareFolder.Text != SettingsWrapper.ShareFolder)
            { SettingsWrapper.ShareFolder = this.textBoxShareFolder.Text; }

            if (this.textBoxPictureSize.Text != SettingsWrapper.MaxPictureSize.ToString())
            { SettingsWrapper.MaxPictureSize = UInt32.Parse(this.textBoxPictureSize.Text); }

            if (this.textBoxP2PAddress.Text != SettingsWrapper.P2PAddress)
            { SettingsWrapper.P2PAddress = this.textBoxP2PAddress.Text; }

            if (this.textBoxP2PPassword.Text != SettingsWrapper.P2PPassword)
            { SettingsWrapper.P2PPassword = this.textBoxP2PPassword.Text; }
        }
    }
}